package com.material.demo.lyric;

import android.text.TextUtils;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by pc on 2016/12/23.
 */

public abstract class BaseParser implements ILyricParser<LyricInfo> {

    private final int CHAR_ARRAY_SIZE = 256;

    @Override
    public void parser(InputStream stream) {
        if (stream != null) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[CHAR_ARRAY_SIZE];
            try {
                while ((len = stream.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }

                String lyricContent = bos.toString();
                if(!TextUtils.isEmpty(lyricContent)){
                    onReadFileCallBack(lyricContent);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bos != null) {
                        bos.close();
                    }

                    if (stream != null) {
                        stream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected void logMsg(String msg) {
        Log.e(getTag(), msg);
    }

    @Override
    public abstract List getLyrics();

    protected abstract void onReadFileCallBack(String content);

    @Override
    public abstract int getOffset();

    protected String getTag(){
        return BaseParser.class.getName();
    }
}
